/**
 * Copyright 2013 Viettel Telecom. All rights reserved.
 * VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.viettel.core.common.utils;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Mo ta muc dich cua lop (interface)
 * 
 * @author: ToanTM1
 * @version: 2.1
 * @since: 2.1
 */
public class MailUtil {
	private static MailUtil instance;
	private String SMTP = "smtp.gmail.com";
	private String PORT = "587";
	private String USERNAME = "touristone.viettel@gmail.com";// Tai khoan de gui
	private String PASSWORD = "12345678a@";// Mat khau tai khoan gui

	public void init(String userName, String password) {
		this.USERNAME = userName;
		this.PASSWORD = password;
	}
	
	public Boolean sendMail(String to, String subject, String body) {
		try {
			Properties props = System.getProperties();
			props.put("mail.smtp.host", SMTP);
			props.put("mail.smtp.port", PORT);
			props.put("mail.smtp.starttls.enable", "true");
			final String login = USERNAME;
			final String pwd = PASSWORD;
			Authenticator pa = null;
			if (login != null && pwd != null) {
				props.put("mail.smtp.auth", "true");
				pa = new Authenticator() {

					public PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(login, pwd);
					}
				};
			}// else: no authentication
			Session session = Session.getInstance(props, pa);
			// — Create a new message –
			Message msg = new MimeMessage(session);
			// — Set the FROM and TO fields –
			msg.setFrom(new InternetAddress(USERNAME));
			msg.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(to, false));

			// — Set the subject and body text –
			msg.setSubject(subject);
			// msg.setText(body);// Để gởi nội dung dạng utf-8 dùng
			// msg.setContent(body, "text/html; charset=UTF-8");
			// — Set some other header information –
			msg.setHeader("X-Mailer", "LOTONtechEmail");
			msg.setContent(body, "text/html; charset=utf-8");
			msg.setSentDate(new Date());
			msg.saveChanges();
			// — Send the message –
			Transport.send(msg);
			System.out.println("Mail da duoc gui");
			return true;
		} catch (Exception ex) {
			return false;
		}
	}

	/**
	 * @return the instance
	 */
	public static MailUtil getInstance() {
		if (instance == null) {
			instance = new MailUtil();
		}
		return instance;
	}
	
	public static void main(String[] args) {
		

		/*StringBuilder contentBuilder = new StringBuilder();
		try {
		    BufferedReader in = new BufferedReader(new FileReader("C:/Users/toantm.GPTHHTHCM/Desktop/tourist_one/email_confirm.html"));
		    String str;
		    while ((str = in.readLine()) != null) {
		        contentBuilder.append(str);
		    }
		    in.close();
		} catch (IOException e) {
		}
		String content = contentBuilder.toString();*/
		
		MailUtil.getInstance().sendMail("paintbsd@gmail.com", "Thu test 2","dddd");
	}

}
